from django.db import models

# Create your models here.


# 品牌表
class Brand(models.Model):
    first_name = models.CharField("品牌首字母", max_length=5, default=None)
    name = models.CharField("品牌的名称", max_length=20)
    logo = models.CharField("品牌logo", max_length=200)

    def __str__(self):
        return self.name

    class Meta:
        db_table = "brand"
        verbose_name_plural = "品牌表"


# 频道组管理
class Cate(models.Model):
    name = models.CharField("种类名称", max_length=30)
    # 自关联： 自己关联自己
    cid = models.ForeignKey('self', related_name='subs', on_delete=models.CASCADE, null=True, blank=True)

    class Meta:
        db_table = 'cate'
        verbose_name_plural = "商品种类"

    def __str__(self):
        return self.name


# 分类分组
# id    name       cate      group_url    sequence
# pk   varchar(30)  1:1    varchar(100)     int
#						默认http://jd.com    0

class Group(models.Model):
    """
    分类分组
    """
    name = models.CharField("频道组名称", max_length=30)
    # 一级类别
    cate = models.OneToOneField(Cate, on_delete=models.CASCADE)
    group_url = models.CharField("频道的地址链接", max_length=100, default="http://jd.com")
    sequence = models.IntegerField(verbose_name="组内的顺序", default=0)

    class Meta:
        db_table = 'cate_group'
        verbose_name_plural = "频道组"

    def __str__(self):
        return self.name


# SPU
# id   pk
# name  varchar(20)  'spu'
# brand    外键 "品牌"
# category1     外键  related_name="cate1_spu"  '一级类别'
# category2
# category3
# comment_num  int  default 0    "评论量"
# sales  int  defult 0   "销量"
# desc_detail  text   "详细描述"
# desc_pack   text  "包装信息"
# desc_service text   '售后服务'
class SPU(models.Model):
    "商品SPU，如小米手机10；苹果13"
    name = models.CharField(max_length=20, verbose_name="spu")
    brand = models.ForeignKey(Brand, on_delete=models.PROTECT, verbose_name='品牌')
    category1 = models.ForeignKey(Cate, on_delete=models.PROTECT, related_name='cate1_spu', verbose_name='一级类别')
    category2 = models.ForeignKey(Cate, on_delete=models.PROTECT, related_name='cate2_spu', verbose_name='二级类别')
    category3 = models.ForeignKey(Cate, on_delete=models.PROTECT, related_name='cate3_spu', verbose_name='三级类别')
    comment_num = models.IntegerField(verbose_name="评论量",default=0)
    sales = models.IntegerField(verbose_name="销量",default=0)
    desc_detail = models.TextField(default='', verbose_name='详细介绍')
    desc_pack = models.TextField(default='', verbose_name='包装信息')
    desc_service = models.TextField(default='', verbose_name='售后服务')

    class Meta:
        db_table = 'spu'
        verbose_name_plural = "SPU表"

    def __str__(self):
        return self.name


# 商品表
# name   varchar(100)
# describe varchar(500)
# price  8,2
# stock int default=0
# sales int default =0
# detail text
# image_default  varchar(300)
# is_selling boolean  True
# cate 外键
# spu 外键
class Good(models.Model):
    """
    商品表
        名称、描述、价格、库存、商品详情、商品默认图片、分类
    """
    name = models.CharField("商品名", max_length=100)
    describe = models.CharField("商品描述", max_length=500)
    price = models.DecimalField("商品价格", max_digits=8, decimal_places=2)
    stock = models.IntegerField("库存", default=0)
    detail = models.TextField("商品详情", default="")
    image_default = models.CharField(verbose_name="默认图片的地址", max_length=300)

    is_selling = models.BooleanField("是否在售", default=True)
    sales = models.IntegerField(verbose_name="销量", default=0)
    # 一级分类
    cate = models.ForeignKey(Cate, on_delete=models.CASCADE, verbose_name="商品类别")
    # 商品sup
    spu = models.ForeignKey(SPU, on_delete=models.CASCADE, verbose_name='商品SPU')

    class Meta:
        db_table = 'sku'
        verbose_name_plural = "商品SKU"

    def __str__(self):
        return self.name


# SPU规格表
# spu 外键  '商品SPU'
# name varchar(20)  "规格名称"
class SPUSpecification(models.Model):
    """商品SPU规格"""
    spu = models.ForeignKey(SPU, on_delete=models.CASCADE, related_name='specs', verbose_name='商品SPU')
    name = models.CharField(max_length=20, verbose_name='规格名称')

    class Meta:
        db_table = 'spu_specification'
        verbose_name_plural = '商品SPU规格'

    def __str__(self):
        return self.name


# 规格选项
# value varchar(20)  '选项值'
# spec 外键（SPUSpecification） '规格'
class SpecOption(models.Model):
    """规格选项"""
    spec = models.ForeignKey(SPUSpecification, related_name='options', on_delete=models.CASCADE, verbose_name='规格')
    value = models.CharField(max_length=20, verbose_name='选项值')

    class Meta:
        db_table = 'specification_option'
        verbose_name_plural = '规格选项'

    def __str__(self):
        return '%s - %s' % (self.spec.name, self.value)


# sku的规格
# sku  外键（Good）
# spec 外键 （SPUSpecification）
# option 外键（SpecificationOption）
class SKUSpecOption(models.Model):
    """SKU具体规格"""
    sku = models.ForeignKey(Good, related_name='specs', on_delete=models.CASCADE, verbose_name='sku商品')
    spec = models.ForeignKey(SPUSpecification, on_delete=models.PROTECT, verbose_name='规格名称')
    option = models.ForeignKey(SpecOption, on_delete=models.PROTECT, verbose_name='规格值')

    class Meta:
        db_table = 'sku_specification'
        verbose_name_plural = '商品规格'

    def __str__(self):
        return '%s: %s - %s' % (self.sku.name, self.spec.name, self.option.value)


# SKU图片
# sku, 商品sku， 引用Good
# image  varchar(300) '商品图片'
class SKUImage(models.Model):
    """SKU图片"""
    sku = models.ForeignKey(Good, on_delete=models.CASCADE, verbose_name='商品')
    image = models.CharField(verbose_name='商品图片', max_length=300)

    class Meta:
        db_table = 'sku_image'
        verbose_name_plural = '商品图片'

    def __str__(self):
        return '%s %s' % (self.sku.name, self.image)
